﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Itfort.Net;
using Newtonsoft.Json;
using QConnectSDK;
using Users.SnsConnect;

namespace Users.WebSite.Login
{
	public partial class SnsAuthCallback : PageBase
	{
		string sns;		// 哪家第三方网站，如：taobao、weibo、qzone、renren
		string code;	// 从第三方网站返回的授权码
		string error;

		protected void Page_Load(object sender, EventArgs e)
		{
			sns = Request.QueryString["sns"];
			code = Request.QueryString["code"];
			error = Request.QueryString["error"];

			if (!String.IsNullOrEmpty(code))
			{
				UserSns userSns = null;

				switch (sns)
				{// 分别处理各家第三方网站认证结果
					case "taobao":
						userSns = TaobaoBack();
						break;
					case "weibo":
						userSns = WeiboBack();
						break;
					case "qzone":
						userSns = QzoneBack();
						break;
					case "renren":
						userSns = RenrenBack();
						break;
					default:
						return;
				}

				if (userSns != null)
				{// 账号验证通过，登陆系统。
					User user = UsersSf.UserService.GetUser(userSns.UserId);
					UserWeb.Login(user);
					UserWeb.RedirectFromLoginPage();
				}
			}
			else
			{

			}
		}

		UserSns TaobaoBack()
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client_Token(OAuth2Utils.Taobao_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			TaobaoToken token = JsonConvert.DeserializeObject<TaobaoToken>(json);
			token.taobao_user_nick = Server.UrlDecode(token.taobao_user_nick);

			UserSns userSns = UsersSf.UserSnsService.GetUserSns(SnsType.Taobao, token.taobao_user_id);

			if (userSns == null)
			{// 注册帐号
				userSns = UsersSf.UserSnsService.CreateUser(SnsType.Taobao, token.taobao_user_id, token.access_token, token.taobao_user_nick);
			}

			return userSns;
		}

		UserSns WeiboBack()
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client_Token(OAuth2Utils.Weibo_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			WeiboToken token = JsonConvert.DeserializeObject<WeiboToken>(json);
			string url = "https://api.weibo.com/2/users/show.json?access_token=" + token.access_token + "&uid=" + token.uid;
			string userJson = HttpClient.RequestContent(new Uri(url), "GET", Encoding.UTF8);
			WeiboUserInfo wbUser = JsonConvert.DeserializeObject<WeiboUserInfo>(userJson);

			UserSns userSns = UsersSf.UserSnsService.GetUserSns(SnsType.Weibo, token.uid);

			if (userSns == null)
			{// 注册帐号
				userSns = UsersSf.UserSnsService.CreateUser(SnsType.Weibo, token.uid, token.access_token, wbUser.screen_name);
			}

			return userSns;
		}

		/// <summary>
		/// QZONE授权成功后的返回处理
		/// </summary>
		/// <returns></returns>
		UserSns QzoneBack()
		{
			// 交换AccessToken并获得QZONE资料
			string state = Guid.NewGuid().ToString().Replace("-", "");
			QOpenClient qOpenClinet = new QOpenClient(code, state);
			var token = qOpenClinet.OAuthToken;
			var qUser = qOpenClinet.GetCurrentUser();

			UserSns userSns = UsersSf.UserSnsService.GetUserSns(SnsType.Qzone, token.OpenId);

			if (userSns == null)
			{// 注册帐号
				userSns = UsersSf.UserSnsService.CreateUser(SnsType.Qzone, token.OpenId, token.AccessToken, qUser.Nickname);
			}

			return userSns;
		}

		UserSns RenrenBack()
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client_Token(OAuth2Utils.Renren_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			RenrenToken token = Newtonsoft.Json.JsonConvert.DeserializeObject<RenrenToken>(json);

			UserSns userSns = UsersSf.UserSnsService.GetUserSns(SnsType.Renren, token.user.id.ToString());

			if (userSns == null)
			{// 注册帐号
				userSns = UsersSf.UserSnsService.CreateUser(SnsType.Renren, token.user.id.ToString(), token.access_token, token.user.name);
			}

			return userSns;
		}
	}
}